new Vue({
  el: '#app',
  data() {
    return {
      rules: {

      },
      form: {
        Sales_Inventory_SerialNumber: "",
        Type_Date: '0',
        Start_Date: '',
        End_Date: '',
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        Children: {
          sales_article_inventory: [{
            Sales_Article_Inventory_SerialNumber: "",
            Sales_Inventory_SerialNumber: "",
            Article_SerialNumber: '', //适用商品
            Distribute: '', //库存数量
          }]
        }
      },
      arr: [{
        value: '0',
        label: '指定分销金额'
      }, {
        value: '1',
        label: '根据订单成交额的百分比'
      }],
      article: [],
      loading: false,
      dialogVisible: false,
      dialogVisible1: false,
      dialogImageUrl: '',
      fileXlsx: [],
      tableData: [],
      dialogVisible: false,//上传
      fileList: [],//文件存放
      tableData: [],//表格预览
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    getData() {
      let _this = this
      $.ajax({
        type: 'POST',
        data: {
          Data: 'article_info'
        },
        url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
        success: function (res) {
          _this.article = res;
          console.log('res', res);
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        },
        dataType: "json"
      });
    },
    add_sales_article_inventory() {
      this.form.Children.sales_article_inventory.push({ //销售价格单适用店铺
        Sales_Article_Inventory_SerialNumber: "",
        Sales_Inventory_SerialNumber: "",
        Article_SerialNumber: '', //适用商品
        Distribute: '', //库存数量
      })
    },
    remove_sales_article_inventory() {
      if (this.form.Children.sales_article_inventory.length > 1) {
        this.form.Children.sales_article_inventory.pop()
      } else {
        this.$message({
          message: '不能在删除了',
          type: 'warning'
        })
      }
    },
    handleDate(e) {//长期短期
      console.log(e);
      if (e == '1') {
        this.form.End_Date = ''
      }
    },
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          $.ajax({
            url: BASE_URL + '/Inventory/addSalesQuantityList',
            type: 'POST',
            data: {
              Data: JSON.stringify({
                sales_inventory: [_this.form]
              }),
              token:$.cookie("token")
            },
            success: function (res) {
              console.log('提交结果', res)
              _this.$message.success("提交成功")
              window.history.go(-1);
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试")
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            }
          })
        } else {
          console.log("error submit!!");
          return false;
        }

      });
    },
    download() {
      window.location.href = './上传模板.xlsx'
    },
    submit() {
      const _this = this
      _this.loading = true
      let obj = {
        Sales_Inventory_SerialNumber: "",
        Type_Date: this.form.Type_Date,
        Start_Date: this.form.Start_Date,
        End_Date: this.form.End_Date,
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
        Children: {
          sales_article_inventory: this.tableData
        }
      }
      console.log(obj);
      $.ajax({
        url: BASE_URL + '/Inventory/addSalesQuantityList',
        type: 'POST',
        data: {
          Data: JSON.stringify({
            sales_inventory: [obj]
          }),
          token:$.cookie("token")
        },
        success: function (res) {
          console.log('提交结果', res)
          _this.$message.success("提交成功")
          window.history.go(-1)
        },
        error: function () {
          _this.$message.error("操作失败，请稍后再试")
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    uploadDetails(file) {
      this.fileList.push({
        name: file.name,
        url: file.type
      })
      const _this = this
      var reader = new FileReader();
      reader.readAsBinaryString(file);
      reader.onload = function (e) {
        var data = e.target.result;
        var wb = XLSX.read(data, {
          type: 'binary' //以二进制的方式读取
        });
        var sheet0 = wb.Sheets[wb.SheetNames[0]];//sheet0代表excel表格中的第一页
        var json = XLSX.utils.sheet_to_json(sheet0);//利用接口实现转换。
        json.forEach(item => {
          _this.tableData.push({
            Sales_Article_Inventory_SerialNumber: "",
            Sales_Inventory_SerialNumber: "",
            Article_SerialNumber: item['商品id']+'-'+$.cookie("enterprise"),
            Article_Name: item['商品名称'],
            Distribute: item['库存数量'],
          })
        })
      }
    },
    beforeRemove() {
      this.fileList.pop()
      this.tableData = []
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

  }
})